﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ContentOptionGroupUI : MonoBehaviour {
    public OptionUI optionPrefab;
    public Transform contentParent;
    public Button addOption;
    public bool useToggleGroup = true;
    /// <summary>
    /// 键：选项；值：对应的选项UI
    /// </summary>
    public Dictionary<OptionContent, OptionUI> optionDic = new Dictionary<OptionContent, OptionUI>();
    public List<OptionUI> optionsUIs = new List<OptionUI>();
    List<OptionUI> sleepyUIs = new List<OptionUI>();
    public bool active
    {
        get
        {
            return _active;
        }
        set
        {
            _active = value;
        }
    }
    bool _active;

    public void ShowOptions(List<OptionContent> options)
    {
        Clear();
        foreach (var item in options)
        {
            AddOneOptionUI(item);
        }
    }

    public void Clear()
    {
        int c = optionsUIs.Count;
        for (int i = 0; i < c; i++)
        {
            optionsUIs[i].Clear();
            sleepyUIs.Add(optionsUIs[i]);
        }
        optionDic.Clear();
        optionsUIs.Clear();
    }

    public OptionUI AddOneOptionUI(OptionContent option)
    {
        OptionUI op;
        if (sleepyUIs.Count > 0)
        {
            op = sleepyUIs[0];
            sleepyUIs.RemoveAt(0);
        }
        else
        {
            op = Instantiate(optionPrefab, contentParent);
        }
        if (useToggleGroup)
        {
            op.toggle.group = contentParent.GetComponent<ToggleGroup>();
        }
        op.SetOptionUI(option);
        optionsUIs.Add(op);
        optionDic.Add(option, op);
        return op;
    }
}
